import json
from apps.merchant.item.dao import ItemDao
from apps.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao
from decimal import Decimal
from apps.system.lessee.dao import LesseeDao
def countFranchiseeAward(order):
    try:
        _order = order
        print('发放加盟店奖励', order.content)
        content = json.loads(_order.content)
        sellingPrice = order.sellingPrice
        discountsRate = Decimal(content.get('discountsRate'))
        franchiseeParams = json.loads(content.get('franchiseeParams'))
        shopParentId = content.get('shopParentId')
        shopUserId = content.get('shopUserId')

        # 赠送购买者锁定积分
        selfSendIncomesLockeds = Decimal(sellingPrice * Decimal(franchiseeParams.get('selfSendIncomesLockedRate'))).quantize(Decimal('0.00'))
        # 加盟商让利，也就是净值
        discountsAward = sellingPrice * discountsRate
        # 加盟商余额
        shop_balance = Decimal(sellingPrice - discountsAward).quantize(Decimal('0.00'))
        # 直推购物积分
        verticalSendIncomes = Decimal(discountsAward * Decimal(franchiseeParams.get('verticalSendIncomesRate'))).quantize(Decimal('0.00'))
        # 日释放池
        freePoolDays = Decimal(discountsAward * Decimal(franchiseeParams.get('dayAwardRate'))).quantize(Decimal('0.00'))
        # 月释放池
        freePoolMonths = Decimal(discountsAward * Decimal(franchiseeParams.get('monthAwardRate'))).quantize(Decimal('0.00'))
        # 运营补贴
        operationAwards = Decimal(discountsAward * Decimal(franchiseeParams.get('operationAwardRate'))).quantize(Decimal('0.00'))
        # 商户推荐人奖励
        shopParentAward = Decimal(discountsAward * Decimal(franchiseeParams.get('shopParentAwardRate'))).quantize(Decimal('0.00'))
        # 创业基金
        helpFunds = Decimal(discountsAward * Decimal(franchiseeParams.get('helpFundRate'))).quantize(Decimal('0.00'))
        # 赠送加盟店锁定积分
        shopSendIncomesLockeds = Decimal(discountsAward * Decimal(franchiseeParams.get('shopSendIncomesLockedRate'))).quantize(Decimal('0.00'))
        # 毛利润
        grossProfits = Decimal(discountsAward - (verticalSendIncomes + freePoolDays + freePoolMonths + operationAwards + shopParentAward + helpFunds)).quantize(Decimal('0.00'))

        # 赠送贡献点
        payway = 'contributionValue'
        # 赠送贡献值
        sendContributionValues = Decimal(sellingPrice / int(franchiseeParams.get('selfSendContributionValueRate'))).quantize(Decimal('0.00'))
        UserDao().addWallet(payway, _order.user, sendContributionValues)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = _order.user.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = sendContributionValues
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

        # 赠送上级贡献点
        payway = 'contributionValue'
        if _order.user.parent:
            verticalSendContributionValues = Decimal(sendContributionValues / 2).quantize(Decimal('0.00'))
            UserDao().addWallet(payway, _order.user.parent, verticalSendContributionValues)
            data = {}
            data['scene'] = 'verticalAward'
            data['user'] = _order.user.parent.id
            data['fromUser'] = _order.user.id
            data['orderId'] = _order.orderId
            data['money'] = verticalSendContributionValues
            data['payway'] = payway
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)
        
        # 赠送商家贡献点
        payway = 'contributionValue'
        if shopUserId:
            shopUser = UserDao().findOneByObjId(shopUserId)
            shopSendContributionValues = Decimal(discountsAward / int(franchiseeParams.get('selfSendContributionValueRate'))).quantize(Decimal('0.00'))
            UserDao().addWallet(payway, shopUser, shopSendContributionValues)
            data = {}
            data['scene'] = 'sendForExpense'
            data['shop'] = _order.shop.id
            data['user'] = shopUserId
            data['fromUser'] = _order.user.id
            data['orderId'] = _order.orderId
            data['money'] = shopSendContributionValues
            data['payway'] = payway
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)

        # 赠送锁定积分
        payway = 'incomesLocked'
        UserDao().addWallet(payway, _order.user, selfSendIncomesLockeds)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = _order.user.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = selfSendIncomesLockeds
        data['moneyLocked'] = selfSendIncomesLockeds
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
        # 赠送商户锁定积分
        payway = 'incomesLocked'
        if shopUserId:
            print('shopSendIncomesLockeds', shopSendIncomesLockeds)
            shopUser = UserDao().findOneByObjId(shopUserId)
            UserDao().addWallet(payway, shopUser, shopSendIncomesLockeds)
            data = {}
            data['scene'] = 'sendForExpense'
            data['shop'] = _order.shop.id
            data['user'] = shopUserId
            data['fromUser'] = _order.user.id
            data['orderId'] = _order.orderId
            data['money'] = shopSendIncomesLockeds
            data['moneyLocked'] = shopSendIncomesLockeds
            data['payway'] = payway
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)
        # 赠送商户余额
        payway = 'shop_balance'
        if shopUserId:
            shopUser = UserDao().findOneByObjId(shopUserId)
            UserDao().addWallet(payway, shopUser, shop_balance)
            data = {}
            data['scene'] = 'sendForExpense'
            data['shop'] = _order.shop.id
            data['user'] = shopUserId
            data['fromUser'] = _order.user.id
            data['orderId'] = _order.orderId
            data['money'] = shop_balance
            data['payway'] = payway
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)

        # 赠送上级购物积分
        payway = 'incomes'
        if _order.user.parent:
            UserDao().addWallet(payway, _order.user.parent, verticalSendIncomes)
            data = {}
            data['scene'] = 'verticalAward'
            data['user'] = _order.user.parent.id
            data['fromUser'] = _order.user.id
            data['orderId'] = _order.orderId
            data['money'] = verticalSendIncomes
            data['payway'] = payway
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)

        # 商户推荐人奖励
        payway = 'incomes'
        if shopParentId:
            shopParent = UserDao().findOneByObjId(shopParentId)
            UserDao().addWallet(payway, shopParent, shopParentAward)
            data = {}
            data['scene'] = 'verticalShopAward'
            data['user'] = shopParentId
            data['fromUser'] = _order.user.id
            data['orderId'] = _order.orderId
            data['money'] = shopParentAward
            data['payway'] = payway
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)


        # 日释放池
        payway = 'freePoolDay'
        LesseeDao().addWallet(payway, freePoolDays)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = ''
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = freePoolDays
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
        # 月释放池
        payway = 'freePoolMonth'
        LesseeDao().addWallet(payway, freePoolMonths)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = ''
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = freePoolMonths
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
        # 运营补贴池
        payway = 'operationAwardPool'
        LesseeDao().addWallet(payway, operationAwards)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = ''
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = operationAwards
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
        # 创业基金池
        payway = 'helpFundPool'
        LesseeDao().addWallet(payway, helpFunds)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = ''
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = helpFunds
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
        # 毛利润
        payway = 'grossProfit'
        LesseeDao().addWallet(payway, grossProfits)
        data = {}
        data['scene'] = 'sendForExpense'
        data['user'] = ''
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = grossProfits
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
        return sendContributionValues
    except Exception as e:
        print(e)
